package com.lzx.iteam;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.lzx.iteam.task.DownloadImageTask;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.PhotoUtil;
import com.lzx.iteam.widget.HackyViewPager;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

/**
 * 图片查看器
 */
public class ImagePagerActivity extends FragmentActivity implements View.OnClickListener {
    private static final String STATE_POSITION = "STATE_POSITION";
    public static final String EXTRA_IMAGE_INDEX = "image_index"; // 图片位置
    public static final String EXTRA_IMAGE_URLS = "image_urls"; // 原图
    public static final String EXTRA_THUME_IMAGES = "thume_images"; // 缩略图

    private HackyViewPager mPager;
    private int pagerPosition;
    private TextView indicator;
    private ArrayList<String> urls;
    private String mThumeImages;
    private String mImagePath;
    private int mImageOrigin;
    //private Bitmap mImageBitmap;
    private int mIndex;

    //private Button mBtnBackToGallery;
    private Button mBtnChooseConfirm;
    private Button mBtnDeleteChoosedImage;
    private Button mBtnSaveToGallery;

    // TODO mImageOrigin Constants.IMAGE_ORIGIN_LOCAL_STRING  本地的地址 传来的图片格式是String
    // TODO mImageOrigin Constants.IMAGE_ORIGIN_LOCAL_ARRAY  本地的地址 传来的图片格式是ArrayList<String>
    // TODO mImageOrigin Constants.IMAGE_ORIGIN_LOCAL_BASE  本地的地址 传来的图片格式是base64
    // TODO mImageOrigin Constants.IMAGE_ORIGIN_URL_ARRAY  url地址 传来的图片格式是ArrayList<String>
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_detail_pager);
        initView();
        pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
        mImageOrigin = getIntent().getIntExtra(Constants.IMAGE_ORIGIN, 0);
        setView(mImageOrigin);


        // TODO 网络加载的图先显示缩略图
        if (mImageOrigin == Constants.IMAGE_ORIGIN_URL_ARRAY) {
            urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
            mThumeImages = getIntent().getStringExtra(EXTRA_THUME_IMAGES);
        }else if (mImageOrigin == Constants.IMAGE_ORIGIN_LOCAL_ARRAY){
            urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
        }else if (mImageOrigin == Constants.IMAGE_ORIGIN_LOCAL_STRING || mImageOrigin == Constants.IMAGE_ORIGIN_LOCAL_BASE) {
            urls = new ArrayList<String>();
            mImagePath = getIntent().getStringExtra(Constants.IMAGE_PATH);
            urls.add(mImagePath);
        }


        mPager = (HackyViewPager) findViewById(R.id.pager);
        ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
        mPager.setAdapter(mAdapter);
        indicator = (TextView) findViewById(R.id.indicator);

        CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
        indicator.setText(text);
        // 更新下标
        mPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
                indicator.setText(text);
                mIndex = arg0;
            }

        });
        if (savedInstanceState != null) {
            pagerPosition = savedInstanceState.getInt(STATE_POSITION);
        }

        mPager.setCurrentItem(pagerPosition);
    }


    /**
     * 初始化
     */
    public void initView() {

        mBtnChooseConfirm = (Button) findViewById(R.id.btn_choose_confirm);
        mBtnChooseConfirm.setOnClickListener(this);
        mBtnDeleteChoosedImage = (Button) findViewById(R.id.btn_delete_choosed_image);
        mBtnDeleteChoosedImage.setOnClickListener(this);
        mBtnSaveToGallery = (Button) findViewById(R.id.btn_sava_to_gallery);
        mBtnSaveToGallery.setOnClickListener(this);

    }

    /**
     * @param origin
     */
    public void setView(int origin) {
        switch (origin) {
            case Constants.IMAGE_ORIGIN_LOCAL_STRING:
                mBtnChooseConfirm.setVisibility(View.VISIBLE);
                mBtnDeleteChoosedImage.setVisibility(View.GONE);
                mBtnSaveToGallery.setVisibility(View.GONE);
                break;
            case Constants.IMAGE_ORIGIN_LOCAL_ARRAY:
                mBtnSaveToGallery.setVisibility(View.GONE);
                mBtnDeleteChoosedImage.setVisibility(View.GONE);
                mBtnChooseConfirm.setVisibility(View.GONE);
                break;
            case Constants.IMAGE_ORIGIN_LOCAL_BASE:
                mBtnSaveToGallery.setVisibility(View.GONE);
                mBtnDeleteChoosedImage.setVisibility(View.GONE);
                mBtnChooseConfirm.setVisibility(View.GONE);
                break;
            case Constants.IMAGE_ORIGIN_URL_ARRAY:
                mBtnSaveToGallery.setVisibility(View.VISIBLE);
                mBtnDeleteChoosedImage.setVisibility(View.GONE);
                mBtnChooseConfirm.setVisibility(View.GONE);
                break;
            default://其它情况均为异常，关闭此界面
                finish();
                break;
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt(STATE_POSITION, mPager.getCurrentItem());
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            /*case R.id.btn_back_to_gallery:
                finish();
                overridePendingTransition(0,R.anim.group_back_out);
                break;*/
            case R.id.btn_choose_confirm:
                if (mImagePath != null && !"".equals(mImagePath)) {
                    Intent confirmIntent = new Intent();
                    //confirmIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    confirmIntent.putExtra(Constants.IMAGE_PATH, mImagePath);
                    //startActivity(confirmIntent);
                    setResult(RESULT_OK, confirmIntent);
                    finish();
                    overridePendingTransition(0, R.anim.group_back_out);
                }
                break;
            case R.id.btn_delete_choosed_image:
//                SelectImageAdapter.mSelectedImage.remove(getIntent().getIntExtra("index",-1));
                finish();
                break;
            case R.id.btn_sava_to_gallery:

                if (mImageOrigin == Constants.IMAGE_ORIGIN_URL_ARRAY) {
                    DownloadImageTask task = new DownloadImageTask(ImagePagerActivity.this,true);
                    task.execute(urls.get(mIndex));
                }
                break;
        }
    }


    private class ImagePagerAdapter extends FragmentStatePagerAdapter {

        public ArrayList<String> fileList;

        public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
            super(fm);
            this.fileList = fileList;
        }

        @Override
        public int getCount() {
            return fileList == null ? 0 : fileList.size();
        }

        @Override
        public Fragment getItem(int position) {
            String url = fileList.get(position);
            String thume = "";
            if (mThumeImages != null){
                String[] images = mThumeImages.split(",");
                thume = images[position];
            }
            return ImageDetailFragment.newInstance(url,thume, mImageOrigin);
        }

    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(0, R.anim.group_back_out);
        super.onBackPressed();
    }
}
